package com.zhang.servicemap.remote;

import com.zhang.internalcommon.constant.AmapConfigConstant;
import com.zhang.internalcommon.dto.PointDTO;
import com.zhang.internalcommon.dto.PointRequest;
import com.zhang.internalcommon.dto.ResponseResult;
import com.zhang.internalcommon.response.TrackResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.nio.charset.StandardCharsets;

/**
 * 类功能描述
 *
 * @author Kye
 * @date 2025-04-15
 */
@Service
public class PointClient {
	@Value("${amap.key}")
	private String amapKey;

	@Value("${amap.sid}")
	private String amapSid;

	@Autowired
	RestTemplate restTemplate;

	public ResponseResult upload (PointRequest pointRequest){
		// &key=<用户的key>
		// 拼装请求的url
		StringBuilder url = new StringBuilder();
		url.append(AmapConfigConstant.POINT_UPLOAD_URL);
		url.append("?");
		url.append("key="+amapKey);
		url.append("&");
		url.append("sid="+amapSid);
		url.append("&");
		url.append("tid="+pointRequest.getTid());
		url.append("&");
		url.append("trid="+pointRequest.getTrid());
		url.append("&");
		url.append("points=");
		PointDTO[] points = pointRequest.getPoints();
		url.append("%5B");
		for (PointDTO p : points
		) {
			url.append("%7B");
			String locatetime = p.getLocatetime();
			String location = p.getLocation();
			url.append("%22location%22");
			url.append("%3A");
			url.append("%22"+location+"%22");
			url.append("%2C");

			url.append("%22locatetime%22");
			url.append("%3A");
			url.append(locatetime);

			url.append("%7D");
		}
		url.append("%5D");

		System.out.println("上传位置请求："+url.toString());
		ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(URI.create(url.toString()), null, String.class);
		System.out.println("上传位置响应："+stringResponseEntity.getBody());

		return ResponseResult.success();

// 		PointDTO[] points = pointRequest.getPoints();
// 		StringBuilder pointsJson = new StringBuilder();
// 		pointsJson.append("[");
// 		for (PointDTO p : points) {
// 			pointsJson.append("{");
// 			pointsJson.append("\"location\":\"").append(p.getLocation()).append("\",");
// 			pointsJson.append("\"locatetime\":").append(p.getLocatetime());
// 			pointsJson.append("},");
// 		}
// 		pointsJson.append("]");
//
// // 使用UriComponentsBuilder自动编码
// 		String url = UriComponentsBuilder.fromHttpUrl(AmapConfigConstant.POINT_UPLOAD_URL)
// 				.queryParam("key", amapKey)
// 				.queryParam("sid", amapSid)
// 				.queryParam("tid", pointRequest.getTid())
// 				.queryParam("trid", pointRequest.getTrid())
// 				.queryParam("points", pointsJson.toString())
// 				.build()
// 				.encode(StandardCharsets.UTF_8) // 自动编码
// 				.toUriString();
//
// 		ResponseEntity<String> response = restTemplate.postForEntity(URI.create(url), null, String.class);
// 		return ResponseResult.success();
	}
}
